package ntu.metaweb.storage;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public abstract class Contribution implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = -1792531053019992192L;
	// map rater to rating
	protected HashMap<String,Rating> rating;
	protected HashSet<String> contributors;
	protected Date timestamp;
	
	protected Contribution() {
		this.contributors = new HashSet<String>();
		this.timestamp = Calendar.getInstance().getTime();
		this.rating = new HashMap<String, Rating>();
	}
	
	public Set<String> getContributors() {
		return this.contributors;
	}
	
	public Date getTimestamp() {
		return this.timestamp;
	}
	
	public int getTotalRating() {
		int total = 0;
		for (Rating r : this.rating.values()) {
			total += r.rating;
		}
		return total;
	}
	
	public double getAverageRating() {
		int total = this.getTotalRating();
		return (total == 0) ? 0 : (total / this.rating.size());
	}
	
	public void addContributor(String contributor) {
		this.contributors.add(contributor);
	}
	
	public abstract void addRating(String rater, int rating);
}
